// $Id: baseworldobject.cpp 470 2008-05-27 16:27:22Z agjermun $
// Implementation of the baseworldobject class
//
// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "baseworldobject.h"
#include "Ogre.h"

using namespace Ogre;

/**
 * complete constructor
 * @param pMgr reference to the SceneManager of the scene
 * @param pNode reference to the SceneNode which contains the object
 * @param world reference to the btDynamicsWorld (Bullet), which contains the object
 * @param mesh filename of the mesh to be loaded for the object
 * @param objectName name given a spesific instance of this class
 * @param position position vector of the object
*/

BaseWorldObject::BaseWorldObject(SceneManager *pMgr,SceneNode *pNode, btDynamicsWorld *world, const String &mesh, const String &objectName, const Vector3 &position)
{
	mMgr = pMgr;
	//mNode = pNode;
	mWorld = world;
	mMesh = mesh;
	mObjectName = objectName;
	mPosition = position;
	
	// Only create one entiy for now
	// XXX: entityCount?
	mEnt = mMgr->createEntity(mObjectName, mMesh);
	
	// figure out size:
	AxisAlignedBox boundingBox = mEnt->getBoundingBox();
	mHalfSize = boundingBox.getHalfSize();

        mNode = pNode->createChildSceneNode(mObjectName, mPosition);

	mNode->attachObject(mEnt);
}

/**
 * constructor with with default mesh and position
 * @param pMgr reference to the SceneManager of the scene
 * @param pNode reference to the SceneNode which contains the object
 * @param world reference to the btDynamicsWorld (Bullet), which contains the object
 * @param objectName name given a spesific instance of this class
*/

BaseWorldObject::BaseWorldObject(SceneManager *pMgr,SceneNode *pNode, btDynamicsWorld *world, const String &objectName)
{
	mMgr = pMgr;
	mNode = pNode;
	mWorld = world;
	mMesh = String();
	mObjectName = objectName;
	mPosition = Vector3(0,0,0);
	
	// Only create one entiy for now
	// XXX: entityCount?
	mEnt = mMgr->createEntity(mObjectName, mMesh);
	
	// figure out size:
	AxisAlignedBox boundingBox = mEnt->getBoundingBox();
	mHalfSize = boundingBox.getHalfSize();

	mNode = pNode->createChildSceneNode(mObjectName, mPosition);

	mNode->attachObject(mEnt);
}

/**
 * destructor
*/
BaseWorldObject::~BaseWorldObject()
{
	mNode->detachObject(mEnt);
	mMgr->destroyEntity(mEnt);
}

/**
 * update the position of the object
*/
void BaseWorldObject::update(Real elapsedTime)
{
	// update physics world position
	// XXX: divide physics and graphics
	btTransform trans;
	btDefaultMotionState *motionState = (btDefaultMotionState*)mBody->getMotionState();
	motionState->getWorldTransform(trans);

	btVector3 pos = trans.getOrigin();

	mNode->setPosition(Vector3(pos.getX(),pos.getY(),pos.getZ()));
	mNode->setOrientation(trans.getRotation().getW(),trans.getRotation().getX(), trans.getRotation().getY(), trans.getRotation().getZ());
}
